using System;
using System.Collections.Generic;
using System.Text;
using Index;
using Index.PostingLists;

namespace Query
{
    public class TermFrequency
    {
        private WikiIndex index;

        public TermFrequency(WikiIndex index)
        {
            this.index = index;
        }

        public int Tf(string word, int document_id)
        {
            object lista = index.GetPostingList(word);

            if (lista != null)
            {
                if (index.Config.IndexType == IndexType.Frequencial)
                {
                    DocumentFrequencyList freq_list = (DocumentFrequencyList)lista;


                    return freq_list.TermFreqency(document_id);
                }

                if (index.Config.IndexType == IndexType.Positional)
                {
                    DocumentPositionList pos_list = (DocumentPositionList) lista;
                    return pos_list.TermFreqency(document_id);
                }
            }
            else
            {
                return 0;
            }
            return 0;
        }

        public int Df(string word)
        {
            return index.DocumentFrequency(word);
        }

        public double IDf(string word)
        {
            int df = Df(word);

            if (df == 0)
            {
                return 0;
            }
            return Math.Log(index.N / df);
        }

        public double Tf_IDf(string word, int document_id)
        {
            return IDf(word) * Tf(word, document_id);
        }
    }
}
